package Day2025_03_24;

public class LeetCode494 {
    public int findTargetSumWays(int[] nums, int target) {
        int n = nums.length;
        int sum = 0;
        for (int x : nums) sum += x;
        sum += target;
        if (sum % 2 != 0) return 0;
        sum /= 2;
        if (sum < 0) return 0;
        int[][] dp = new int[n + 1][sum + 1];
        // for (int i = 0; i <= sum; i++) dp[0][i] = -1;
        dp[0][0] = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j <= sum; j++) {
                dp[i][j] = dp[i - 1][j];
                if (j >= nums[i - 1]) dp[i][j] += dp[i - 1][j - nums[i - 1]];
            }
        }
        return dp[n][sum];
    }
}
